/**
 * Created by ahwwl on 17-7-28.
 */
(function($){
    var form;
    layui.use(['form'], function(){
        form = layui.form();
        //全选
        form.on('checkbox(allChoose)', function(data){
            var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]');
            child.each(function(index, item){
                item.checked = data.elem.checked;
            });
            form.render('checkbox');
        });


    });
    $.fn.tabletree=function(p)
    {
        p = $.extend({columns:[],data:[],parems:{},expanded:false,emptyMsg:"empty"},p);
        var table = $('<table class="table table-striped dataTable no-footer" lay-skin="line"></table>').appendTo($(this));
        var tbody = $('<tbody></tbody>').appendTo(table);
        var g = {
            init:function () {
                g.setData(g.settings.data);
                g.load(0);
                table.delegate(".webix_tree","click",function () {
                    var obj = $(this).parents("tr");
                    var ttid = obj.attr("data-ttid");
                    var expanded = obj.attr("data-expanded");
                    if (expanded == "false")
                    {
                        obj.attr("data-expanded",true);
                        g.hidePid(ttid,true);
                        $(this).removeClass("webix_tree_close").addClass("webix_tree_open");
                    }else{
                        obj.attr("data-expanded",false);
                        g.hidePid(ttid,false);
                        $(this).removeClass("webix_tree_open").addClass("webix_tree_close");
                    }
                });

            },
            setData:function (data) {
                var parems = {};
                var datas = {};
                for(var i=0;i<data.length;i++)
                {
                    if (!parems["ppid_"+data[i].ppid])
                    {
                        parems["ppid_"+data[i].ppid] = [];
                    }

                    parems["ppid_"+data[i].ppid].push(data[i]);
                    datas["ttid_"+data[i].ttid] = data[i];
                }
                g.settings.data = datas;
                g.settings.parems = parems;
            },
            getData1:function (ttid) {

                var data1 = [];
                if(g.settings.data["ttid_"+ttid])
                {
                    data1 = g.settings.data["ttid_"+ttid];
                }

                return data1;
            },
            load:function (pid) {

                g.removePid(pid);
                var data = g.setParem(g.settings.parems,pid,1);
                var obj = table.find("tr[data-ttid='"+pid+"']");
                var tbodytr = null;
                var td = null;
                var tdspan = null;
                var obj_level = 0;
                if(obj.length>0)
                {
                    obj_level = obj.attr("data-level");
                    var data1 = g.getData1(pid);
                    g.setTr(data1);
                }
                for(var i=0;i<data.length;i++)
                {
                    if(obj.length>0)
                    {
                        obj.after('<tr></tr>');
                        tbodytr = obj.next();
                        obj = tbodytr;
                        data[i].level = parseInt(obj_level) +data[i].level;
                    }else{
                        tbodytr = $('<tr></tr>').appendTo(tbody);
                    }
                    var obj1 = tbody.find("tr[data-ttid='"+data[i].ppid+"']");
                    var  expanded = "false";
                    if(obj1.length>0)
                    {
                        expanded = obj1.attr("data-expanded");
                    }
                    tbodytr.attr({"data-ttid":data[i].ttid,"data-ppid":data[i].ppid,"data-level":data[i].level});
                    for(var f =0;f<data[i].list.length;f++)
                    {
                        td = $("<td ></td>").appendTo(tbodytr);
                        tdspan = $('<span class="treetablevalue"></span>"').appendTo(td);
                        tdspan.append(data[i].list[f]);
                    }
                    if(data[i].expanded != undefined)
                    {

                        if(data[i].expanded === true)
                        {
                            tbodytr.find("td").eq(0).prepend('<span class="webix_tree_open webix_tree"></span>');
                        }else
                        {
                            tbodytr.find("td").eq(0).prepend('<span class="webix_tree_close webix_tree"></span>');
                        }

                        tbodytr.attr({"data-expanded":data[i].expanded});
                    }
                    tbodytr.find("td").eq(0).css("padding-left",data[i].level*15+"px");
                    if(expanded == "false")
                    {
                        tbodytr.hide();
                    }

                }
                // if(g.settings.expanded == false && pid <1)
                // {
                //     tbody.find("tr").hide();
                //
                // }
                tbody.find("tr[data-ppid=0]").show();
            },
            setTr:function (data) {
                if(data )
                {
                    var obj = table.find("tr[data-ttid='"+data.ttid+"']");
                    for(var i =0;i<data.list.length;i++)
                    {
                        obj.find("td").eq(i).find(".treetablevalue").html(data.list[i]);
                    }
                }

            },

            setParem:function (parems,ppid,level) {

                // console.log(ppid,parems)
                var data = [];
                if(!parems["ppid_"+ppid])
                {
                    return data;
                }


                for(var i =0;i<parems["ppid_"+ppid].length;i++)
                {
                    if(parems["ppid_"+ppid][i])
                    {

                        var ttid = parems["ppid_"+ppid][i].ttid;
                        parems["ppid_"+ppid][i].level = level;
                        var data1 = g.setParem(parems,ttid,(level+1));
                        if(data1.length)
                        {

                            parems["ppid_"+ppid][i].expanded=g.settings.expanded;
                        }
                        data.push(parems["ppid_"+ppid][i]);
                        data = data.concat(data1);
                    }
                }
                // if(ppid == 5)
                // {
                //     console.log(data);
                // }
                // delete parems[ppid];
                return data;
            },
            hidePid:function (pid,V) {
                var obj = tbody.find("tr[data-ppid='"+pid+"']");
                for(var i=0;i<obj.length;i++)
                {
                    var ttid = obj.eq(i).attr("data-ttid");
                    g.hidePid(ttid,false);
                    if(V)
                    {
                        obj.eq(i).show();
                    }else{
                        obj.eq(i).hide();
                        if(obj.eq(i).attr("data-expanded"))
                        {
                            obj.eq(i).find(".webix_tree").removeClass("webix_tree_open").addClass("webix_tree_close");
                            obj.eq(i).attr("data-expanded","false")
                        }

                    }
                }
                // if(V)
                // {
                //     tbody.find("tr[data-ttid='"+pid+"'][data-expanded] .webix_tree").removeClass("webix_tree_close").addClass("webix_tree_open").attr("data-expanded",true);
                // }else{
                //     tbody.find("tr[data-ttid='"+pid+"'][data-expanded] .webix_tree").removeClass("webix_tree_open").addClass("webix_tree_close").attr("data-expanded",false);
                // }
            },
            removePid:function (pid) {
                var obj = tbody.find("tr[data-ppid='"+pid+"']");
                for(var i=0;i<obj.length;i++)
                {
                    var ttid = obj.eq(i).attr("data-ttid");
                    g.removePid(ttid);
                    obj.eq(i).remove();
                }
            }
        };
        g.settings = p;
        $(this).each(function () {
            this.grid = g;
        });
        g.init();
        // console.log(this.grid)
        return g

    };
    $.fn.setData = function (data) {

        return this.each(function () {

            this.grid.setData(data);

        });
    };
    $.fn.load = function (pid) {

        return this.each(function () {

            this.grid.load(pid);

        });
    };
    $.fn.removePid = function (pid) {

        return this.each(function () {

            this.grid.removePid(pid);

        });
    };
    // $.fn.reload = function () {
    //     return this.each(function () {
    //         this.grid.ajax();
    //     });
    // }
})(jQuery);